package com.fth.learn.chapter04;

import java.util.Arrays;

/**
 * 学习一下调整为数组
 * YiMi
 *
 * @author YiMi
 * @version 1.0
 * @date 2023/5/20 13:26
 */

public class LearnArray {


    public static void main(String[] args) {
//        [类型][]标识符[][=][new][类型][长度]；
        char[] c1 = new char[3];
        System.out.println(Arrays.toString(c1));
        String a = null;
        System.out.println(a);
        int[] int1 = new int[4];
        //默认值
//        int1现在里面{0}{0}{0}{0}
        int1[0] = 2;
        int1[1] = 2;
        int1[2] = 2;
        int1[3] = 2;


//        int[] int2 = {1,2,3,4,5,6};
//        //默认开辟六个{1}{2}{3}{4}{5}{6}
//        System.out.println(Arrays.toString(int2));
//        //正序
//        for(int i=1;i<int2.length;i++){
//            int2[i] = int2[i] + int2[i-1];
//        }
//        System.out.println(Arrays.toString(int2));
        //倒叙
//        for(int i = int2.length-1;i>0;i--){
//            int2[i] = int2[i] + int2[i-1];
//        }
//        System.out.println(Arrays.toString(int2));


        int[] arr2 = new int[]{2, 5, 1};

//        int max = 1;
//        int temp = arr2[max];
//        arr2[max] = arr2[arr2.length - 1];
//        arr2[arr2.length-1] = temp;

        //加法
        int c = 9;
        int b = 13;
        b = b + c;
        c = b - c;
        b = b - c;
        System.out.println(c);
        System.out.println(b);
        //减法


        //排序算法

        //交换排序

        bubble(arr2);
        choose(arr2);

    }

    /**
     * 冒泡
     */

    public static void bubble(int[] arr2) {
        for (int j=0;j<arr2.length-1;j++){
            for (int i = 0; i < arr2.length - 1- j ; i++) {
                if (arr2[i] > arr2[i + 1]) {
                    //交换
    //                    int temp = arr2[i];
    //                    arr2[i] = arr2[i+1];
    //                    arr2[i+1] = temp;
                    arr2[i] = arr2[i] + arr2[i + 1];
                    arr2[i + 1] = arr2[i] - arr2[i + 1];
                    arr2[i] = arr2[i] - arr2[i + 1];

                }
            }
        }
        System.out.println(Arrays.toString(arr2));
    }

    /**
     * 选择排序
     */
    public static void choose(int[] arr2) {
        //求几次最大值
        for (int j = 0; j < arr2.length - 1; j++) {
            //计数器
            //求最大值
            int maxVal = Integer.MIN_VALUE;
            int index = 0;
            for (int i = 0; i < arr2.length - j; i++) {
                if (maxVal < arr2[i]) {
                    maxVal = arr2[i];
                    index = i;
                }
            }
            int temp = arr2[index];
            //交换
            arr2[index] = arr2[arr2.length - 1 - j];
            arr2[arr2.length - 1 - j] = temp;
        }
        System.out.println(Arrays.toString(arr2));
    }

}
